package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendShiftAddDTO;
import org.jsola.hr.dto.AttendShiftUpdateDTO;
import org.jsola.hr.query.AttendShiftQuery;
import org.jsola.hr.service.IAttendShiftService;
import org.jsola.hr.vo.AttendShiftListVO;
import org.jsola.hr.vo.AttendShiftVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * @author zhr
 */
@RestController("hrAttendShiftController")
@RequestMapping(HrConstants.API_PATH + "/v1/attendShifts")
public class AttendShiftController implements IAttendShiftControllerAPI {

    @Autowired
    private IAttendShiftService attendShiftService;

    @Override
    @PostMapping
    public Result<AttendShiftVO> save(@Valid @RequestBody AttendShiftAddDTO attendShiftAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增考勤班次权限校验

        return Result.success(
                attendShiftService.save(attendShiftAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AttendShiftUpdateDTO attendShiftUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改考勤班次权限校验

        int count = attendShiftService.update(attendShiftUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{attendShiftId}")
    public Result<Integer> delete(@PathVariable String attendShiftId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除考勤班次权限校验

        int count = attendShiftService.deleteByIds(tokenUser, attendShiftId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] attendShiftIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除考勤班次权限校验

        int count = attendShiftService.deleteByIds(tokenUser, attendShiftIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{attendShiftId}")
    public Result<AttendShiftVO> findById(@PathVariable String attendShiftId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看考勤班次权限校验

        return Result.success(
            attendShiftService.selectById(attendShiftId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AttendShiftListVO>> listAll(@Valid AttendShiftQuery attendShiftQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看考勤班次权限校验

        return Result.success(
            attendShiftService.select(attendShiftQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<AttendShiftListVO>> page(@Valid AttendShiftQuery attendShiftQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            attendShiftService.selectPage(attendShiftQuery, tokenUser.getSiteId())
        );
    }


}

